<!-- 引入管台账 -->
<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="引入管编号" prop="pipeId">
        <el-input
          v-model="queryParams.pipeId"
          placeholder="请输入引入管编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="班组" prop="teamId">
        <el-select v-model="queryParams.teamId" placeholder="请选择班组" clearable>
          <el-option
            v-for="dict in dict.type.bus_person_group"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="负责人员" prop="personId">
        <el-select v-model="queryParams.personId" placeholder="请选择负责人员" clearable>
          <el-option
            v-for="dict in dict.type.bus_person_name"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="运行日期" prop="runDate">
        <el-date-picker clearable
          v-model="queryParams.runDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择运行日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['business:pipe:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['business:pipe:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['business:pipe:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['business:pipe:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar  :showSearch.sync="showSearch" @queryTable="getList" :columns="columns"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="pipeList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" type="index" width="100" align="center" v-if="getColumn('index')">
        <template slot-scope="scope">
          <span>{{(queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1}}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="主键" align="center" prop="id" v-if="getColumn('id')" /> -->
      <el-table-column label="引入管编号" align="center" prop="pipeId" v-if="getColumn('pipeId')" />
      <el-table-column label="引入管数量" align="center" prop="pipeNumber" v-if="getColumn('pipeNumber')" />
      <el-table-column label="架空管长度" align="center" prop="pipeLength" v-if="getColumn('pipeLength')" />
      <el-table-column label="所属街道" align="center" prop="streetId" v-if="getColumn('streetId')" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.bus_neighborhoodledger" :value="scope.row.streetId"/>
        </template>
      </el-table-column>
      <el-table-column label="所属小区" align="center" prop="neighborId" v-if="getColumn('neighborId')" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.bus_neighborhood" :value="scope.row.neighborId"/>
        </template>
      </el-table-column>
      <el-table-column label="班组" align="center" prop="teamId" v-if="getColumn('teamId')" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.bus_person_group" :value="scope.row.teamId"/>
        </template>
      </el-table-column>
      <el-table-column label="负责人员" align="center" prop="personId" v-if="getColumn('personId')" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.bus_person_name" :value="scope.row.personId"/>
        </template>
      </el-table-column>
      <el-table-column label="运行日期" align="center" prop="runDate" width="180" v-if="getColumn('runDate')" >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.runDate, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" v-if="getColumn('operate')" >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['business:pipe:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['business:pipe:remove']"
          >删除</el-button>
        
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :pageSizes="pageSizes"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改引入管台账对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="引入管编号" prop="pipeId">
          <el-input v-model="form.pipeId" placeholder="请输入引入管编号" />
        </el-form-item>
        <el-form-item label="引入管数量" prop="pipeNumber">
          <el-input v-model="form.pipeNumber" placeholder="请输入引入管数量" />
        </el-form-item>
        <el-form-item label="架空管长度" prop="pipeLength">
          <el-input v-model="form.pipeLength" placeholder="请输入架空管长度" />
        </el-form-item>
        <el-form-item label="所属街道" prop="streetId">
          <el-select v-model="form.streetId" placeholder="请选择所属街道">
            <el-option
              v-for="dict in dict.type.bus_neighborhoodledger"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="所属小区" prop="neighborId">
          <el-select v-model="form.neighborId" placeholder="请选择所属小区">
            <el-option
              v-for="dict in dict.type.bus_neighborhood"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="详细地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入详细地址" />
        </el-form-item>
        <el-form-item label="经度" prop="longitude">
          <el-input v-model="form.longitude" placeholder="请输入经度" />
        </el-form-item>
        <el-form-item label="纬度" prop="latitude">
          <el-input v-model="form.latitude" placeholder="请输入纬度" />
        </el-form-item>
        <el-form-item label="班组" prop="teamId">
          <el-select v-model="form.teamId" placeholder="请选择班组"  @change="getStaffList">
            <el-option
              v-for="dict in dict.type.bus_person_group"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="负责人员" prop="personId">
          <el-select v-model="form.personId" placeholder="请选择负责人员">
            <el-option
              v-for="item in staffList"
              :key="item.id"
              :label="item.personName"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="运行日期" prop="runDate">
          <el-date-picker clearable
            v-model="form.runDate"
            type="datetime" value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="请选择运行日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listPipe, getPipe, delPipe, addPipe, updatePipe } from "@/api/business/pipe";
import { getTeamPerson } from "@/api/business/person";
export default {
  name: "Pipe",
  dicts: ['bus_person_name', 'bus_person_group','bus_neighborhoodledger','bus_neighborhood'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 引入管台账表格数据
      pipeList: [],
       // 对应班组的所有员工
       staffList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        pipeId: null,
        teamId: null,
        personId: null,
        runDate: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        pipeId: [
          { required: true, message: "引入管编号不能为空", trigger: "blur" }
        ],
        streetId: [
          { required: true, message: "所属街道不能为空", trigger: "blur" }
        ],
        neighborId: [
          { required: true, message: "所属小区不能为空", trigger: "blur" }
        ],
        teamId: [
          { required: true, message: "班组不能为空", trigger: "change" }
        ],
        personId: [
          { required: true, message: "负责人员不能为空", trigger: "change" }
        ],
      },
      // 列信息
      columns: [
        { key: 'index', label: `序号`, visible: true },
        { key: 'id', label: '主键', visible: true },
        { key: 'pipeId', label: '引入管编号', visible: true },
        { key: 'pipeNumber', label: '引入管数量', visible: true },
        { key: 'pipeLength', label: '架空管长度（m）', visible: true },
        { key: 'streetId', label: '所属街道', visible: true },
        { key: 'neighborId', label: '所属小区', visible: true },
        { key: 'teamId', label: '班组', visible: true },
        // { key: 'personId', label: '负责人员', visible: true },
        { key: 'runDate', label: '运行日期', visible: true },
        { key: 'operate', label: `操作`, visible: true }
      ],
      // 分页参数
      pageSizes:[10,50,100,500,1000]
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询引入管台账列表 */
    getList() {
      this.loading = true;
      listPipe(this.queryParams).then(response => {
        this.pipeList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 获取对应班组的员工
    getStaffList() {
      let teamId = this.form.teamId;
      console.log("teamId:", teamId);
       getTeamPerson(teamId).then((response) => {
        // console.log("response:", response);
        this.staffList = response.data;
      console.log("staffList:", this.staffList);
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        delFlag: null,
        createUser: null,
        createBy: null,
        createTime: null,
        updateUser: null,
        updateBy: null,
        updateTime: null,
        pipeId: null,
        pipeNumber: null,
        pipeLength: null,
        streetId: null,
        neighborId: null,
        address: null,
        longitude: null,
        latitude: null,
        teamId: null,
        personId: null,
        runDate: null,
        remark: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加引入管台账";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getPipe(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改引入管台账";
        this.getStaffList();
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updatePipe(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPipe(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除选中的数据项？').then(function() {
        return delPipe(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('business/pipe/export', {
        ...this.queryParams
      }, `pipe_${new Date().getTime()}.xlsx`)
    },
    /** 列是否显示 */
    getColumn(key){
      const res = this.columns.filter(item => item.key === key);
      if(res.length >= 1){
        return res[0].visible
      } else {
        return false
      }
    }
  }
};
</script>
